package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketOrderGoods;
import com.cskaoyan.bean.bo.WxOrderCommentBo;
import com.cskaoyan.bean.bo.WxOrderSubmitBo;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.AdminOrderListVo;
import com.cskaoyan.bean.vo.WxOrderDetailVo;
import com.cskaoyan.bean.vo.WxOrderListVo;
import com.cskaoyan.bean.vo.WxOrderSubmitVo;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.service.WxOrderService;
import com.cskaoyan.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.Map;

/**
 * @Since 2023/01/02 00:58
 * @Author LA
 * @Description: 微信小程序端订单相关功能
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {
    @Autowired
    WxOrderService wxOrderService;
    @Autowired
    OrderService orderService;

    /****
     *  微信小程序，订单评论功能
     * @param wxOrderCommentBo
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author LA
     * @since 2023/01/02 1:04
     */
    @RequestMapping(value = "comment", method = RequestMethod.POST)
    // Request URL: http://182.92.235.201:8083/wx/order/comment
    public BaseRespVo comment(@RequestBody WxOrderCommentBo wxOrderCommentBo) {
        // get  orderGoodsId 订单商品的id
        Integer goodsId = wxOrderCommentBo.getOrderGoodsId();

        BaseRespVo respVo = BaseRespVo.ok();
        // 两种情况
        // 先排除掉已经评价的 订单？商品？
        // 订单商品评论，如果是-1，则超期不能评价；如果是0，则可以评价；如果其他值，则是comment表里面的评论ID
        Integer commentInt = orderService.selectOrderGoodsCommentById(goodsId);
        // 先处理空值情况
        if (commentInt == null) {
            respVo.setErrno(400);
            return respVo;
        }
        if (commentInt == -1) {
            respVo.setErrno(400);
            respVo.setErrmsg("用户您好，该订单商品已超过时间，不能评价了！");
            return respVo;
        }
        // 如果大于0的其他值，则是comment表里面的评论ID。
        if (commentInt > 0) {
            respVo.setErrno(400);
            respVo.setErrmsg("用户您好，该订单商品不能评价了！");
            return respVo;
        }
        // 只有等于0的时候才可以评价
        // 评价操作
        orderService.insertComment(wxOrderCommentBo);
        // 成功响应 {errno: 0, errmsg: "成功"}
        respVo.setErrno(0);
        respVo.setErrmsg("成功");
        return respVo;
    }

    /****
     * wx小程序，订单列表展示页面
     * showType展示类型： 0 全部；1 待付款；2代发货；3待收货；4待评价
     *
     * 订单状态对应的状态码orderStatus
     *  101: '未付款',
     *  102: '用户取消',103: '系统取消',
     *  201: '已付款',
     *  202: '申请退款', 203: '已退款',
     *  301: '已发货',
     *  401: '用户收货', 402: '系统收货'
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author LA
     * @since 2023/01/02 2:03
     */
    @RequestMapping("list")
    // showType: 0 page: 1 limit: 10
    public BaseRespVo list(Short showType, BasePageInfo pageInfo) {
        CommonData<WxOrderListVo> data = orderService.wxList(showType, pageInfo);
        return BaseRespVo.ok(data, "成功");

    }

    // 订单详情
    @RequestMapping("detail")
    // Request URL: http://182.92.235.201:8083/wx/order/detail?orderId=513
    public BaseRespVo detail(String orderId) {
        // id的长度校验
        if (StringUtil.isEmpty(orderId)) {
            BaseRespVo baseRespVo = BaseRespVo.invalidParameter("参数值不对");
            // 参数违规
            baseRespVo.setErrno(402);
            return baseRespVo;
        }
        WxOrderDetailVo data = orderService.wxDetail(orderId);
        return BaseRespVo.ok(data, "成功");

    }

    // Request URL: http://182.92.235.201:8083/wx/order/goods?orderId=490&goodsId=1181013
    @RequestMapping("goods")
    public BaseRespVo detail(Integer orderId, Integer goodsId) {
        if (orderId != null && goodsId != null) {
            MarketOrderGoods data = orderService.wxGoods(orderId, goodsId);
            return BaseRespVo.ok(data, "成功");
        }
        return BaseRespVo.errMsgAndNo(402, "参数不合法,存在关键数据为空");

    }

    // Request URL: http://182.92.235.201:8083/wx/order/delete
    @RequestMapping("delete")
    public BaseRespVo detail(@RequestBody Map map) {
        // {orderId: 506}
        Integer orderId = (Integer) map.get("orderId");
        orderService.wxDelete(orderId);
        // {"errno":0,"errmsg":"成功"}
        return new BaseRespVo().ok("成功");
    }

    // Request URL: http://182.92.235.201:8083/wx/order/refund
    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map map) {
        // {orderId: 606}
        Integer orderId = (Integer) map.get("orderId");
        // 申请退款
        orderService.wxRefund(orderId);
        return BaseRespVo.ok("成功");
    }

    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody Map map) {

        Integer orderId = (Integer) map.get("orderId");
        // 申请取消
        orderService.wxCancel(orderId);
        return BaseRespVo.ok("成功");
    }

    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        // 确认收货
        orderService.wxConfirm(orderId);
        return BaseRespVo.ok("成功");
    }

    // Request URL: http://182.92.235.201:8083/wx/order/prepay
    @RequestMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        // 付款
        orderService.wxPrepay(orderId);
        return BaseRespVo.ok("成功");
    }

    // Request URL: http://182.92.235.201:8083/wx/order/submit
    @RequestMapping("submit")
    public BaseRespVo submit(WxOrderSubmitBo orderSubmitBo/*, HttpSession session*/) {
        // 填值
        WxOrderSubmitVo data = orderService.wxSubmit(orderSubmitBo/*, session*/);
        // {"errno":0,"data":{"orderId":609,"grouponLinkId":0},"errmsg":"成功"}
        if (data == null) {
            return BaseRespVo.errMsgAndNo(400, "参数错误，无法继续进行");
        }
        return BaseRespVo.ok(data, "成功");
    }
}


